package com.bardsoftware.foronuvolo.data;

import java.util.ArrayList;
import java.util.List;

public class SearchDiscussionChunk implements DiscussionChunk {
    private final List<Discussion> myDiscussions;
    private final int myStartResult;
    private final String myQueryHash;
    private final boolean hasNextPage;

    public SearchDiscussionChunk(List<String> searchResults, int startResult, String queryHash) {
        myDiscussions = new ArrayList<Discussion>();
        for (int i = startResult; i < searchResults.size() && myDiscussions.size() <= 10; i++) {
            Discussion d = Discussion.find(Long.valueOf(searchResults.get(i)));
            if (d != null) {
                myDiscussions.add(d);
            }
        }
        myStartResult = startResult;
        myQueryHash = queryHash;
        hasNextPage = startResult + 10 < searchResults.size();
    }

    public List<Discussion> getDiscussions() {
        return myDiscussions;
    }

    public String getID() {
        return String.valueOf(myStartResult) + "_" + myQueryHash;
    }

    public String getNextChunkID() {
        return hasNextPage ? String.valueOf(myStartResult + 10) : "";
    }

    public String getPrevChunkID() {
        return myStartResult >= 10 ? String.valueOf(myStartResult - 10) : "";
    }

}
